package com.ht.managermentserver;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ht.managermentserver.entity.BaseEntity;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/**
 * 代码生成工具
 *
 * <p>生成代码到 generator 文件夹下
 *
 * @author czy
 * @package com.ht.managermentserver
 * @date 2020/5/29 9:35
 */
@Slf4j
public class AutoGeneratorTool {

  @SuppressWarnings("SpellCheckingInspection")
  public static void main(final String[] args) throws JsonProcessingException {

    final String mysql_Connection_String =
      "jdbc:mysql://localhost:3306/ht_manager_system?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
//    final String mysql_Connection_String =
//        "jdbc:mysql://localhost:3306/dje_dev_report?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
    final ObjectMapper objectMapper = new ObjectMapper();
    log.info("##### 开始生成 ......");
    final AutoGenerator autoGenerator = new AutoGenerator();
    // 配置要生成的表
    final String[] genderList = new String[]{"sys_dic_type"};
    if (genderList == null || genderList.length == 0) {
      log.warn("$$$$$$ 参数错误,没有要生成的实体表");
      return;
    }
    log.info("##### 本次生成的表" + objectMapper.writeValueAsString(genderList));
    // 1.全局配置

    final GlobalConfig gc = new GlobalConfig();
    final String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(
      projectPath + "/generator/"); // managermentserver\src\main\java\com\ht\managermentserver
    gc.setAuthor("czy");
    gc.setOpen(false);
    gc.setFileOverride(true);
    gc.setServiceName("%sService");
    gc.setSwagger2(true); // 实体属性 Swagger2 注解

    autoGenerator.setGlobalConfig(gc);

    // 2. 设置db连接
    final DataSourceConfig dataSourceConfig = new DataSourceConfig();

    dataSourceConfig
      .setUrl(mysql_Connection_String)
      .setUsername("root")
      .setPassword("Dje-dev-2019")
      .setDriverName("com.mysql.cj.jdbc.Driver");

    autoGenerator.setDataSource(dataSourceConfig);

    // 3. 包的配置

    final PackageConfig packageConfig = new PackageConfig();

    packageConfig
      .setController("controller")
      .setEntity("entity")
      .setMapper("mapper")
      .setService("service")
      .setParent("com.ht")
      .setModuleName("managermentserver");

    autoGenerator.setPackageInfo(packageConfig);
    // 4.策略的配置 策略(strategy)

    final StrategyConfig strategyConfig = new StrategyConfig();
    strategyConfig.setInclude(genderList); // 要生成的表实体
    strategyConfig.setNaming(NamingStrategy.underline_to_camel);
    strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
    strategyConfig.setEntityLombokModel(true);
    strategyConfig.setSuperEntityClass(BaseEntity.class);
    strategyConfig.setEnableSqlFilter(true);
    strategyConfig.setEntityBuilderModel(true);
    strategyConfig.setEntityColumnConstant(true);

    strategyConfig.setLogicDeleteFieldName("deleted");
    strategyConfig.setSuperControllerClass("com.ht.managermentserver.controller.BaseController");
    // 5.自动填充配置

    final TableFill create_name_table_fill = new TableFill("create_date", FieldFill.INSERT);
    final TableFill create_Id_table_fill = new TableFill("create_id", FieldFill.INSERT);
    final TableFill update_name_table_fill = new TableFill("update_date", FieldFill.INSERT_UPDATE);
    final TableFill update_Id_table_fill = new TableFill("update_id", FieldFill.INSERT_UPDATE);
    final TableFill version_table_fill = new TableFill("version", FieldFill.INSERT);
    final TableFill deleted_table_fill = new TableFill("deleted", FieldFill.INSERT);
    strategyConfig.setTableFillList(
      Arrays.asList(
        create_name_table_fill,
        update_name_table_fill,
        version_table_fill,
        deleted_table_fill,
        create_Id_table_fill,
        update_Id_table_fill));

    strategyConfig.setVersionFieldName("version");
    strategyConfig.setRestControllerStyle(true);
    strategyConfig.setControllerMappingHyphenStyle(true); // 根据表名建立对应的类名

    autoGenerator.setStrategy(strategyConfig);

    autoGenerator.execute();

    log.info("##### 已完成生成任务 ......");
  }
}
